package com.jsh.ex.service;

import com.jsh.ex.model.SampleChannel;
import com.jsh.ex.model.SampleContent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wukai on 2017/10/16.
 */
@Service
public class WeiboRssService {

    public Map<String, Object> produceRssByUid(String uid) {
        Map<String, Object> info = new HashMap<String,Object>();

        SampleChannel channel = new SampleChannel();
        List<SampleContent> list = new ArrayList<SampleContent>();

        Document doc = null;
        try {
            String url = "http://service.weibo.com/widget/widget_blog.php?uid="+uid;
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if(doc != null){
            channel.setTitle(doc.title());
            channel.setDescription(doc.title() + " - 通过Rss Hunter获取【https://gitee.com/accacc/rsshunter】");
            channel.setLink("");

            Elements items = doc.select(".wgtCell_con");

            for (Element item : items) {
                SampleContent content = new SampleContent();
                String itemContent = item.select(".wgtCell_txt").html();
                content.setSummary(itemContent);

                String itemTitle = item.select(".wgtCell_txt").text();
                content.setTitle(itemTitle);

                String itemTime = item.select(".wgtCell_txtBot .wgtCell_tm a").text();
                //TODO 处理时间
                content.setCreatedDate(null);

                String itemUrl = item.select(".wgtCell_txtBot .wgtCell_tm a").attr("href");
                content.setUrl(itemUrl);

                list.add(content);
            }
        } else {
            return null;
        }
        info.put("channel", channel);
        info.put("items", list);
        return info;
    }

}
